package com.thingcom.delinrobot.view;

import android.content.Context;
import android.content.res.TypedArray;

import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;



import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;


import com.thingcom.delinrobot.R;


public class SimpleToolbar extends Toolbar {
    private static final String TAG = "SimpleToolbar";
    private ImageView myLeftIcon;
    private ImageView myRightIcon1;
    private ImageView myRightIcon2;
    private TextView myRightText;
    private TextView myTitle;
    //private Toolbar myToolbar;

    private int leftIconId;
    private int rightIcon1Id;
    private int rightIcon2Id;
    private int rightTextColor;
    private String title;
    private String rightText;

    //private RelativeLayout leftClickLayout;


    public SimpleToolbar(Context context) {
        this(context,null);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public SimpleToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleToolbar);
        leftIconId = a.getResourceId(R.styleable.SimpleToolbar_left_icon_back,-1);
        rightIcon1Id = a.getResourceId(R.styleable.SimpleToolbar_right_icon_1,-1);
        rightIcon2Id = a.getResourceId(R.styleable.SimpleToolbar_right_icon_2,-1);
        title = a.getString(R.styleable.SimpleToolbar_title_text);
        rightText = a.getString(R.styleable.SimpleToolbar_right_text);
        rightTextColor =  a.getColor(R.styleable.SimpleToolbar_right_text_color,
                getResources().getColor(R.color.front_blue));

        a.recycle();

        LayoutInflater.from(context).inflate(R.layout.simple_toolbar_layout,this,true);
        myLeftIcon =findViewById(R.id.toolbar_back);
        myTitle = findViewById(R.id.toolbar_title);
        myRightIcon1 = findViewById(R.id.toolbar_first_icon);
        myRightIcon2 = findViewById(R.id.toolbar_second_icon);
        myRightText = findViewById(R.id.toolbar_right_text);
       // myToolbar = findViewById(R.id.toolbar);

        setLeftIcon();
        setRightIcon1();
        setRightIcon2();
        setTitle();
        setRightText();
    }

    private void setLeftIcon(){
        if (leftIconId == -1){ //没有图标
            myLeftIcon.setVisibility(View.GONE);
        }else {
            myLeftIcon.setVisibility(VISIBLE);
            myLeftIcon.setClickable(true);
            myLeftIcon.setFocusable(true);
            myLeftIcon.setImageResource(leftIconId);
            //myLeftIco
        }
    }

    public void setMyLeftIcon(@DrawableRes int iconId){
        leftIconId = iconId;
        setLeftIcon();
    }

    private void setRightIcon2(){
        if (rightIcon2Id == -1){
            myRightIcon2.setVisibility(View.GONE);
        }else {
            myRightIcon2.setImageResource(rightIcon2Id);
        }
    }

    public void setMyRightIcon1(@DrawableRes int iconId){
        rightIcon1Id = iconId;
        setRightIcon1();
    }
    private void setRightIcon1(){
        if (rightIcon1Id == -1){
            myRightIcon1.setVisibility(View.GONE);
        }else {
            myRightIcon1.setVisibility(VISIBLE);
            myRightIcon1.setClickable(true);
            myRightIcon1.setFocusable(true);
            myRightIcon1.setImageResource(rightIcon1Id);
        }
    }

    public void setTitle(){
        if (title == null){
            myTitle.setVisibility(View.GONE);
        }else {
            myTitle.setText(title);
        }
    }

    public void setTitleStyle(int Color,float Size){
        if (title == null){
            myTitle.setVisibility(View.GONE);
        } else {
            myTitle.setTextColor(Color);
            myTitle.setTextSize(Size);
        }
    }
    public void setTitle(@NonNull String title){
        if (myTitle != null){
            myTitle.setText(title);
        }
    }

    public void setRightText(){
        if (rightText == null){
            myRightText.setVisibility(View.GONE);
        }else {
            myRightText.setText(rightText);
            myRightText.setTextColor(rightTextColor);
        }
    }

    public void setRightText(String rightText){
        if (myRightText !=null){
            myRightText.setText(rightText);
        }
    }

    public void setLeftIconClickListener(View.OnClickListener listener){
        if (myLeftIcon != null){
            myLeftIcon.setOnClickListener(listener);
        }
    }


    public void setRightIcon1ClickListener(View.OnClickListener listener){
        if (myRightIcon1 != null){
            myRightIcon1.setOnClickListener(listener);
        }
    }


    public void setRightIcon2ClickListener(View.OnClickListener listener){
        if (myRightIcon2 != null){
            myRightIcon2.setOnClickListener(listener);
        }
    }

    public void setRightTextClickListener(View.OnClickListener listener){
        if (myRightText != null){
            myRightText.setOnClickListener(listener);
        }
    }

    public ImageView getMyLeftIcon() {
        return myLeftIcon;
    }

    public ImageView getMyRightIcon1() {
        return myRightIcon1;
    }

    public ImageView getMyRightIcon2() {
        return myRightIcon2;
    }

    public TextView getMyRightText() {
        return myRightText;
    }
}
